package org.anyin.gitee.cloud.center.auth.api;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.anyin.gitee.cloud.lib.core.cache.CacheTemplate;
import org.anyin.gitee.cloud.lib.permission.PermissionRedisConstant;
import org.anyin.gitee.cloud.lib.permission.PermissionRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class AutoPermissionRoleServiceImpl implements PermissionRoleService {

    @Autowired
    private CacheTemplate cacheTemplate;

    @Override
    public List<String> getPermissionByLoginId(String loginId) {
        String key = PermissionRedisConstant.DEFAULT_USER_PERMISSION_PREFIX + loginId;
        return cacheTemplate.valueGetList(key, String.class);
    }

    @Override
    public List<String> getRoleByLoginId(String loginId) {
        String key = PermissionRedisConstant.DEFAULT_USER_ROLE_PREFIX + loginId;
        return cacheTemplate.valueGetList(key, String.class);
    }

    @Override
    public void clear(String loginId) {
        cacheTemplate.keyRemove(Lists.newArrayList(
                PermissionRedisConstant.DEFAULT_USER_PERMISSION_PREFIX + loginId,
                PermissionRedisConstant.DEFAULT_USER_ROLE_PREFIX + loginId
        ));
    }
}
